// ignore_for_file: depend_on_referenced_packages

import 'package:flutter_2b_client/json/LocalDateTimeConverter.dart';
import 'package:flutter_2b_client/model/IntIdAndName.dart';
import 'package:flutter_2b_client/utils/local_datetime.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:retrofit/http.dart';
import 'package:dio/dio.dart';

import '../utils/Page.dart';
import 'Initial.dart';
import 'MarketOrderApi.dart';

part 'MarketDeliveryOrderApi.g.dart';

part 'MarketDeliveryOrderApi.freezed.dart';

@RestApi()
abstract class MarketDeliveryOrderApi {

  factory MarketDeliveryOrderApi(Dio dio, {String baseUrl}) = _MarketDeliveryOrderApi;

  @GET("/markets/{id}/deliveryOrders")
  Future<Slice<QueryMarketDeliveryOrderInMarketResponse>> queryMarketDeliveryOrderInMarket(
      @Path("id") int marketId, Pageable pageable);

  @GET("/markets/deliveryOrders/{orderId}")
  Future<MarketDeliveryOrderDetail> getMarketDeliveryOrderDetail(@Path("orderId") int orderId);

  @GET("/markets/deliveryOrders/{orderId}/items")
  Future<Page<OrderItemInOrderSummary>> queryOrderItemsInOrder(
      @Path("orderId") int orderId, @Queries() Map<String, dynamic> queries);

  @DELETE("/markets/deliveryOrders/{orderId}/items/{goodsId}")
  Future<void> deleteOrderItem(
      @Path("orderId") int orderId, @Path("goodsId") int goodsId);

  @PATCH("/markets/deliveryOrders/{orderId}/items/{goodsId}/amount")
  Future<void> updateOrderItemGoodsAmount(
      @Path("orderId") int orderId,
      @Path("goodsId") int goodsId,
      @Body() UpdateItemAmountRequest updateItemAmountRequest);

  @GET("/markets/deliveryOrders/{orderId}/items/barcodes/{barcode}/exists")
  Future<bool> exists(
      @Path("orderId") int orderId, @Path("barcode") String barcode);

  @POST("/markets/deliveryOrders/{orderId}/items/barcodes/{barcode}")
  Future<OrderItemInOrderSummary> addItemFromBarcode(
      @Path("orderId") int orderId, @Path("barcode") String barcode);

  @PATCH("/markets/deliveryOrders/{orderId}/validate")
  Future<void> validateMarketDeliveryOrder(@Path("orderId") int orderId);

  @POST("/markets/deliveryOrders/{orderId}/items/goods/{goodsId}")
  Future<OrderItemInOrderSummary> addItemFromGoodsId(
      @Path("orderId") int orderId, @Path("goodsId") int goodsId);

  @POST("/markets/{marketId}/deliveryOrders")
  Future<int> createOrder(@Path("marketId") int marketId);

  @PATCH("/markets/deliveryOrders/{orderId}/confirm")
  Future<void> confirmMarketDeliveryOrder(@Path("orderId") int orderId);

}

@freezed
class UpdateItemAmountRequest with _$UpdateItemAmountRequest {
  const factory UpdateItemAmountRequest({
    required int amount,
    int? unitId
  }) = _UpdateItemAmountRequest;

  const UpdateItemAmountRequest._();

  factory UpdateItemAmountRequest.fromJson(Map<String, dynamic> json) =>
      _$UpdateItemAmountRequestFromJson(json);
}

@freezed
class OrderItemInOrderSummary with _$OrderItemInOrderSummary {
  const factory OrderItemInOrderSummary({
    required int goodsId,
    required String name,
    required String barcode,
    required int amountInOrder,
    required int inStore,
    String? boxSpecification,
    IntIdAndName? unit,
    @Default([])
    List<QueryGoodsUnitResponse> units,
  }) = _OrderItemInOrderSummary;

  const OrderItemInOrderSummary._();

  factory OrderItemInOrderSummary.fromJson(Map<String, dynamic> json) =>
      _$OrderItemInOrderSummaryFromJson(json);
}

@freezed
class MarketDeliveryOrderDetail with _$MarketDeliveryOrderDetail {
  const factory MarketDeliveryOrderDetail({
    required int id,
    @LocalDateTimeConverter() required LocalDateTime createdDateTime,
    required IntIdAndName market,
    @LocalDateTimeConverter() LocalDateTime? validatedDateTime,
    @LocalDateTimeConverter() LocalDateTime? auditedDateTime,
  }) = _MarketDeliveryOrderDetail;

  const MarketDeliveryOrderDetail._();

  factory MarketDeliveryOrderDetail.fromJson(Map<String, dynamic> json) =>
      _$MarketDeliveryOrderDetailFromJson(json);
}

MarketDeliveryOrderApi marketDeliveryOrderApi = MarketDeliveryOrderApi(dio, baseUrl: baseUrl);

@freezed
class QueryMarketDeliveryOrderInMarketResponse with _$QueryMarketDeliveryOrderInMarketResponse {
  const factory QueryMarketDeliveryOrderInMarketResponse(
      {required int id,
      required IntIdAndName createdBy,
      @LocalDateTimeConverter() required LocalDateTime createdDateTime,
      @LocalDateTimeConverter() LocalDateTime? lastModifiedDateTime,
      required int goodsKindAmount}) = _QueryMarketDeliveryOrderInMarketResponse;

  const QueryMarketDeliveryOrderInMarketResponse._();

  factory QueryMarketDeliveryOrderInMarketResponse.fromJson(
          Map<String, dynamic> json) =>
      _$QueryMarketDeliveryOrderInMarketResponseFromJson(json);
}
